﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    class Fireball
    {
        public Texture2D sprite;
        public Vector2 position;
        public float rotation;
        public Vector2 center;
        public Vector2 velocity;
        public bool alive, first = true, once = true;
        public Vector2 startPosition;

        private SoundEffect fireball;
        private SoundEffect explosion;

        private AnimationPlayer player;
        //private AnimationPlayer player;
        Level level;
        private Animation fireballCollision;

        //private Animation fireballCollision;

        public bool collided = false;

        public bool checkAlive
        {
            get { return alive; }
        }

        public void LoadContent()
        {
            fireballCollision = new Animation(level.Content.Load<Texture2D>("Sprites/EnemySkills/fireExplosion"), 0.1f, false);

            // Load sounds.            
            fireball = level.Content.Load<SoundEffect>("Sounds/Fireball");
            explosion = level.Content.Load<SoundEffect>("Sounds/FireExplosion");
        }

        public Rectangle rectangle
        {
            get
            {
                int left = (int)position.X;
                int width = sprite.Width;
                int top = (int)position.Y;
                int height = sprite.Height;
                return new Rectangle(left, top, width, height);
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (alive)
                spriteBatch.Draw(sprite, position, Color.White);

            if (collided)
                player.Draw(gameTime, spriteBatch, new Vector2(position.X, position.Y + 40), SpriteEffects.None);
        }

        public Fireball(Level l, Vector2 pos, int dir)
        {
            this.level = l;
            rotation = 0.0f;

            position = new Vector2(pos.X, pos.Y - 40);
            startPosition = position;
            if (dir == -1)
            {
                velocity = new Vector2(-8.5f, 0.0f);
                sprite = level.Content.Load<Texture2D>("Sprites/EnemySkills/Fire1");
            }
            else
            {
                velocity = new Vector2(8.5f, 0.0f);
                sprite = level.Content.Load<Texture2D>("Sprites/EnemySkills/Fire2");
            }

            center = new Vector2(sprite.Width / 2, sprite.Height / 2);

            alive = true;
            LoadContent();
        }

        public void UpdateFireball()
        {
            //Only update them if they're alive
            if(alive)
            {
                if (first == true)
                {
                    fireball.Play();
                    first = false;
                }
                //Move our fireball based on it's velocity
                position += velocity;

                //Rectangle the size of the screen so fireballs that
                //fly off screen are deleted.

                Rectangle screenRect = new Rectangle(0, 0, 800, 600);
                if (!screenRect.Contains(new Point(
                    (int)position.X,
                    (int)position.Y)))
                {
                    alive = false;
                    collided = true;
                }

                //Collision rectangle for each fireball -Will also be
                //used for collisions with enemies.
                Rectangle fireballRect = new Rectangle(
                    (int)position.X,
                    (int)position.Y,
                    sprite.Width,
                    sprite.Height);

                //Check for collisions with the player
                    if (fireballRect.Intersects(level.Player.BoundingRectangle))
                    {
                        level.Player.currentHP -= 20;
                        alive = false;
                        collided = true;
                    }



                //Look for adjacent tiles to the fireball
                Rectangle bounds = new Rectangle(
                    fireballRect.Center.X,
                    fireballRect.Center.Y,
                    fireballRect.Width / 4,
                    fireballRect.Height / 4);
                int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
                int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
                int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
                int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

                // For each potentially colliding tile
                for (int y = topTile; y <= bottomTile; ++y)
                {
                    for (int x = leftTile; x <= rightTile; ++x)
                    {
                        TileCollision collision = level.GetCollision(x, y);

                        //If we collide with an Impassable or Platform tile
                        //then delete our fireball.
                        if (collision == TileCollision.Impassable ||
                            collision == TileCollision.Platform)
                        {
                            if (fireballRect.Intersects(bounds))
                            {
                                alive = false;
                                collided = true;
                            }
                        }
                    }
                }
            }
            if (collided)
            {
                player.PlayAnimation(fireballCollision);
                if (once == true)
                {
                    explosion.Play();
                    once = false;
                }
            }

        }
    }
}
